/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.omega_r.omegarecyclerview.swipe_menu_example;

import com.omega_r.libs.omegarecyclerview.base.BaseBean;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

public class ContactsViewHolder extends BaseViewHolder {
    private Text nameTextView;
    private Button messageButton;
    private Text editTextView;
    private Text deleteTextView;
    private Image voiceMailButton;
    private Image bubbleChartButton;

    public ContactsViewHolder(Component itemView, ListContainer listContainer, int leftXmlId, int rightXmlId) {
        super(itemView, listContainer, leftXmlId, rightXmlId);
        nameTextView = (Text) findComponentById(ResourceTable.Id_text_contact_name);
        messageButton = (Button) findComponentById(ResourceTable.Id_button_message);
        editTextView = (Text) findComponentById(ResourceTable.Id_text_edit);
        editTextView.setClickedListener(this);
        deleteTextView = (Text) findComponentById(ResourceTable.Id_text_delete);
        deleteTextView.setClickedListener(this);
        voiceMailButton = (Image) findComponentById(ResourceTable.Id_imagebutton_voicemail);
        voiceMailButton.setClickedListener(this);
        bubbleChartButton = (Image) findComponentById(ResourceTable.Id_imagebutton_bubble);
        bubbleChartButton.setClickedListener(this);
    }

    @Override
    public void updateView(BaseBean baseBean) {
        super.updateView(baseBean);
        Contacts contact = (Contacts) baseBean;
        nameTextView.setText(contact.getName());
        messageButton.setVisibility(contact.isOnline() ? Component.VISIBLE : Component.INVISIBLE);
    }

    @Override
    public void onClick(Component component) {
        super.onClick(component);
        switch (component.getId()) {
            case ResourceTable.Id_text_edit:
                showToast("Edit");
                break;
            case ResourceTable.Id_text_delete:
                showToast("Delete");
                break;
            case ResourceTable.Id_imagebutton_voicemail:
                showToast("Voice mail");
                break;
            case ResourceTable.Id_imagebutton_bubble:
                showToast("Bubble");
                break;
        }
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    private void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(itemView.getContext())
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(itemView.getContext())
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }
}
